package com.wan.data.mysql.orm.meta;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wan.data.mysql.exception.DBInitializeException;

import java.util.HashSet;
import java.util.Set;

public class TableMeta {
    /** 对象类型 */
    private final Class<?> entityType;
    /** 数据库名称 */
    private final String databaseName;
    /** 表名 */
    private final String tableName;
    /** 自增起始值 */
    private final int autoBegin;
    /** 主键 */
    private ColumnMeta pkColumn;
    /** 自增字段 */
    private ColumnMeta autoColumn;
    /** 字段列表 */
    private final Set<ColumnMeta> columnMetas;
    /** 索引字段列表 */
    private final Set<IndexMeta> indexColumnMetas;

    public TableMeta(Class<?> entityType, String databaseName, String tableName,int beginValue) {
        this.entityType = entityType;
        this.databaseName = databaseName;
        this.tableName = StrUtil.isEmpty(tableName) ? StrUtil.toUnderlineCase(entityType.getSimpleName()) : tableName;
        this.columnMetas = new HashSet<>();
        this.indexColumnMetas = new HashSet<>();
        this.autoBegin = beginValue;
    }

    public Class<?> getEntityType() {
        return entityType;
    }

    public String getDatabaseName() {
        return databaseName;
    }

    public String getTableName() {
        return tableName;
    }

    public ColumnMeta getPkColumn() {
        return pkColumn;
    }

    public void setPkColumn(ColumnMeta pkColumn) {
        this.pkColumn = pkColumn;
    }

    public Set<ColumnMeta> getColumnMetas() {
        return columnMetas;
    }

    public Set<IndexMeta> getIndexColumnMetas() {
        return indexColumnMetas;
    }

    public ColumnMeta getAutoColumn(){
        return autoColumn;
    }

    public int getAutoBegin() {
        return autoBegin;
    }

    public void addColumnMeta(ColumnMeta columnMeta){
        if(columnMeta.isPk()){
            if(pkColumn != null && pkColumn.isPk()){
                throw new DBInitializeException("实体类型" + entityType.getName() + "的主键暂时不支持设置多个");
            }
            pkColumn = columnMeta;
        }
        if(columnMeta.isAutoIncrement()){
            if(autoColumn != null && autoColumn.isAutoIncrement()){
                throw new DBInitializeException("实体类型" + entityType.getName() + "自增字段不能设置多个");
            }
            Class<?> fieldType = Convert.wrap(columnMeta.getFieldType());
            if (!Number.class.isAssignableFrom(fieldType)) {
                throw new DBInitializeException("实体类型" + entityType.getName() + "的自增字段" + columnMeta.getFieldName() + "必须是数字类型");
            }
            autoColumn = columnMeta;
        }
        /*if(columnMeta.isIndex()){
            indexColumnMetas.add(columnMeta);
        }*/
        columnMetas.add(columnMeta);
    }

    public void addIndexMeta(IndexMeta indexMeta){
        if(indexColumnMetas.stream().anyMatch(i->i.getName().equals(indexMeta.getName()))){
            throw new DBInitializeException(entityType.getCanonicalName() + "索引名不能重复:" + indexMeta.getName());
        }
        indexColumnMetas.add(indexMeta);
    }

    public ColumnMeta[] getOperateColumns(boolean containAuto){
        return columnMetas.stream().filter(columnMeta -> containAuto || !columnMeta.isAutoIncrement()).toArray(ColumnMeta[]::new);
    }

    public String[] getOperateColumnNames(boolean containAuto){
        return columnMetas.stream().filter(columnMeta -> containAuto || !columnMeta.isAutoIncrement())
                .map(ColumnMeta::getColumnName).toArray(String[]::new);
    }

}
